/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Prophed.java
 *
 * Created on 24.12.2009, 08:21:11
 */
package com.googlecode.prophed;

import com.kenai.paranoidray.shared.SwingUtils;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import java.awt.Color;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 *
 * @author Ray
 */
public class Prophed extends javax.swing.JFrame {

    Binding b = new Binding();
    GroovyShell gs = new GroovyShell(b);
    ImagePanel imagePanel;

    /** Creates new form Prophed */
    public Prophed() {
        initComponents();
        setLocationRelativeTo(null);
        //eventPanel.addPaintListener(myPL);
        new Thread(new Runnable() {

            public void run() {
                gs.evaluate("");
            }
        }).start();


        imagePanel = new ImagePanel();
        Scaffold s = new Scaffold(imagePanel);
        jScrollPaneImage.setViewportView(s);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPaneImage = new javax.swing.JScrollPane();
        jToolBarTop = new javax.swing.JToolBar();
        jButtonExecute = new javax.swing.JButton();
        jToggleButton1 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jButtonColor = new javax.swing.JButton();
        jPanelCode = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPaneCode = new javax.swing.JScrollPane();
        jTextAreaCode = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jMenuBarTop = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        jMenuItemOpen = new javax.swing.JMenuItem();
        jMenuItemSave = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(800, 600));
        getContentPane().add(jScrollPaneImage, java.awt.BorderLayout.CENTER);

        jToolBarTop.setFloatable(false);
        jToolBarTop.setRollover(true);

        jButtonExecute.setText("Execute Code");
        jButtonExecute.setFocusable(false);
        jButtonExecute.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonExecute.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonExecute.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExecuteActionPerformed(evt);
            }
        });
        jToolBarTop.add(jButtonExecute);

        buttonGroup1.add(jToggleButton1);
        jToggleButton1.setText("Select");
        jToggleButton1.setFocusable(false);
        jToggleButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarTop.add(jToggleButton1);

        buttonGroup1.add(jToggleButton2);
        jToggleButton2.setSelected(true);
        jToggleButton2.setText("Paint");
        jToggleButton2.setFocusable(false);
        jToggleButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jToggleButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBarTop.add(jToggleButton2);

        jButtonColor.setText("Color");
        jButtonColor.setFocusable(false);
        jButtonColor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButtonColor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButtonColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonColorActionPerformed(evt);
            }
        });
        jToolBarTop.add(jButtonColor);

        getContentPane().add(jToolBarTop, java.awt.BorderLayout.PAGE_START);

        jPanelCode.setLayout(new java.awt.BorderLayout());

        jTextAreaCode.setColumns(20);
        jTextAreaCode.setRows(5);
        jTextAreaCode.setText("int i=0;\nfor( i=0; i< 100; i+=4)\nimage.getGraphics().drawOval(233, i, 70, 70);");
        jScrollPaneCode.setViewportView(jTextAreaCode);

        jTabbedPane1.addTab("tab1", jScrollPaneCode);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("imagePanel.cutTopLine();\nimagePanel.cutLeftLine();\nimagePanel.cutBottomLine();\nimagePanel.cutRightLine();");
        jScrollPane1.setViewportView(jTextArea1);

        jTabbedPane1.addTab("tab2", jScrollPane1);

        jTextArea2.setColumns(20);
        jTextArea2.setRows(5);
        jTextArea2.setText("import java.awt.image.BufferedImage;\nBufferedImage bitemp = new BufferedImage( image.getWidth(), image.getHeight()-1, BufferedImage.TYPE_INT_ARGB);\nbitemp.getGraphics().drawImage(image, 0, -1, null);\nimagePanel.setImage(bitemp);\nimagePanel.calcSize();");
        jScrollPane2.setViewportView(jTextArea2);

        jTabbedPane1.addTab("tab3", jScrollPane2);

        jPanelCode.add(jTabbedPane1, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(jPanelCode, java.awt.BorderLayout.PAGE_END);

        jMenuFile.setText("File");

        jMenuItemOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemOpen.setText("Open");
        jMenuItemOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemOpenActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemOpen);

        jMenuItemSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItemSave.setText("Save");
        jMenuItemSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItemSaveActionPerformed(evt);
            }
        });
        jMenuFile.add(jMenuItemSave);

        jMenuBarTop.add(jMenuFile);

        jMenuEdit.setText("Edit");
        jMenuBarTop.add(jMenuEdit);

        setJMenuBar(jMenuBarTop);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonExecuteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExecuteActionPerformed
        // TODO add your handling code here:

        //b.setVariable("i", myPL.getImage());
        b.setVariable("imagePanel", imagePanel);
        b.setVariable("image", imagePanel.getImage());
        b.setVariable("graphics2D", (Graphics2D) imagePanel.getImage().getGraphics());
        //myPL.getImage().getGraphics().drawOval(10, 10, 10, 10);
        JScrollPane sp = (JScrollPane) jTabbedPane1.getSelectedComponent();
        JTextArea ta = (JTextArea) sp.getViewport().getView();
        gs.evaluate(ta.getText());
        jScrollPaneImage.getViewport().getView().repaint();
        //eventPanel.repaint();
    }//GEN-LAST:event_jButtonExecuteActionPerformed

    private void jMenuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemOpenActionPerformed
        // TODO add your handling code here:
        JFileChooser jfc = new JFileChooser();
        int showOpenDialog = jfc.showOpenDialog(this);
        if (showOpenDialog == JFileChooser.APPROVE_OPTION) {
            try {
                imagePanel = new ImagePanel(ImageIO.read(jfc.getSelectedFile()));
                Scaffold s = new Scaffold(imagePanel);
                jScrollPaneImage.setViewportView(s);
                jScrollPaneImage.repaint();
            } catch (IOException ex) {
                Logger.getLogger(Prophed.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jMenuItemOpenActionPerformed

    private void jButtonColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonColorActionPerformed
        // TODO add your handling code here:

        Color newColor = JColorChooser.showDialog(
                this,
                "Choose Color",
                imagePanel.getColor());
        imagePanel.setColor(newColor);
        jButtonColor.setForeground(newColor);
        //Background(newColor);
    }//GEN-LAST:event_jButtonColorActionPerformed

    private void jMenuItemSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSaveActionPerformed
        try {
            // TODO add your handling code here:
            ImageIO.write(imagePanel.getImage(), "png", new File("c:\\RayTemp\\test.png"));
        } catch (IOException ex) {
            Logger.getLogger(Prophed.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }//GEN-LAST:event_jMenuItemSaveActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                SwingUtils.setPlatLAF();
                new Prophed().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonColor;
    private javax.swing.JButton jButtonExecute;
    private javax.swing.JMenuBar jMenuBarTop;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenuItem jMenuItemOpen;
    private javax.swing.JMenuItem jMenuItemSave;
    private javax.swing.JPanel jPanelCode;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPaneCode;
    private javax.swing.JScrollPane jScrollPaneImage;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JTextArea jTextAreaCode;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToolBar jToolBarTop;
    // End of variables declaration//GEN-END:variables
}
